#!/usr/bin/python
import sys, os 
import pygst 
pygst.require("0.10") 
import gst 
import pygtk 
pygtk.require('2.0') 
import gtk, gtk.glade 
import gobject

PIPELINE="""filesrc location=%s ! decodebin ! audioconvert ! tee name=tee1  
tee1.src0 
! queue ! volume name=vol0 ! audioconvert ! 
halaudiosink udi=/org/freedesktop/Hal/devices/usb_device_41e_3015_noserial_if0_alsa_playback_0  
tee1.src1 
! queue !  volume name=vol1 !  audioconvert ! alsasink device=hw:0 
tee1.src2 ! queue !  volume name=vol2 ! audioconvert ! 
halaudiosink udi=/org/freedesktop/Hal/devices/usb_device_77d_7af_noserial_if0_alsa_playback_0
"""

class SixFX:
    def __init__(self):
        # Setup the GUI from the Glade file 
        gladeFile = os.path.join(os.path.dirname(__file__,), '6fx.glade') 
        self.xml = gtk.glade.XML(gladeFile, None) 
        self.xml.signal_autoconnect(self) 
        window = self.xml.get_widget("main") 
        self.scale = self.xml.get_widget("vscale1") 
        window.connect('delete-event', gtk.main_quit) 
        window.show_all() 
 
        # Create the pipeline 
        self.pipeline = gst.parse_launch(PIPELINE % (sys.argv[1]))
        self.vol0 = self.pipeline.get_by_name('vol0')
        self.vol1 = self.pipeline.get_by_name('vol1')
        self.vol2 = self.pipeline.get_by_name('vol2')
        self.pipeline.set_state(gst.STATE_PLAYING)


    def on_playButton_clicked(self, widget):
        # Start the pipeline
        self.pipeline.set_state(gst.STATE_PLAYING)

    def on_pauseButton_clicked(self, widget):
        # Pause the pipeline
        self.pipeline.set_state(gst.STATE_PAUSED)
            
    def on_masterScale_value_changed(self, w):
        scale = w.get_value() 
        self.vol0.set_property('volume', scale/100)
        self.vol1.set_property('volume', scale/100)
        self.vol2.set_property('volume', scale/100)

    def on_goodbyeButton_clicked(self, widget):
        vol = self.vol1.get_property('volume')
        gobject.timeout_add(100, self.fade)

    def fade(self):
        vol = self.vol1.get_property('volume')
        vol *= 0.97
        if vol < 0.01: sys.exit(0)
        self.vol0.set_property('volume', vol)
        self.vol1.set_property('volume', vol)
        self.vol2.set_property('volume', vol)
        self.scale.set_value(100*(vol))
        gobject.timeout_add(100, self.fade)
        
    def on_quit_activate(self, widget):
        gtk.main_quit()

if __name__ == "__main__":
    six=SixFX()
    gtk.main()
